// pages/task/task.js
const db=wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid:"",
    shownum:0,
    task_s:"全部任务",
    task:[],
    showShare: false,
    options: [
      { name: '微信', icon: 'wechat', openType: 'share' },
      { name: '微博', icon: 'weibo' },
      { name: '复制链接', icon: 'link' },
      { name: '分享海报', icon: 'poster' },
      { name: '二维码', icon: 'qrcode' },
    ],
    starttime:[],
    endtime:[],
    actions: [
    {
      name:'全部任务',
    },
  ],
    value:"",
    value1:"",
    show:[false,false,false,false,false],
    tishi:false,
    showjianyi:false,
    memsage:"建议你摆烂",
    showxuanxiang:false,
    taskid:1,
  },
  taskms(e){
    wx.setStorageSync('taskms',e.target.id)
    wx.navigateTo({
      url: '../tasktalk/tasktalk',
    })
  },
  onClick11(event) {
    this.setData({ showShare: true });
  },

  onClose11() {
    this.setData({ showShare: false });
  },

  onSelect11(event) {
    
    this.onClose11();
  },
  creatpretask(){
    this.setData({tishi:false})
    var id=this.data.taskid;
    
    wx.setStorageSync('taskid', id);

    wx.navigateTo({
      url: '../addpersontask/addpersontask',
    })
  },
  creatgrouptask(){
    this.setData({tishi:false})
    wx.navigateTo({
      url: '../addgrouptask/addgrouptask',
    })
  },
  onClosecreatgrouptask(){
    this.setData({creatgrouptask:false});
  },
  onSearch(event){
    for(var index in this.data.task){
      let item='task['+index+'].hid';
      this.setData({[item]:"block"});
    }
    for( var index in this.data.task){
      if(this.data.task[index].taskname!=event.detail){
         let item1='task['+index+'].hid';
        this.setData({[item1]:"none"});
      }
    }
    this.setData({task_s:event.detail});
  },
  onCancel(){
    for(var index in this.data.task){
      let item='task['+index+'].hid';
      this.setData({[item]:"block"});
    }
    this.setData({task_s:"全部任务"});
  },
  onshaixuan(){
    this.setData({showxuanxiang:true})
  },
  onClosexuanxiang() {
    this.setData({ showxuanxiang: false });
  },

  onSelectxuanxiang(event) {
    for(var index in this.data.task){
        let item='task['+index+'].hid';
        this.setData({[item]:"block"});
    }
    this.setData({task_s:event.detail.name})
    if(event.detail.name!="全部任务"){
      for(var index in this.data.task){
        var st=false;
      for( var index1 in this.data.task[index].target){
          if(this.data.task[index].target[index1]==event.detail.name){
            st=true;
          } 
      }    
      if(!st){
        let item='task['+index+'].hid';
        this.setData({[item]:"none"});
      }
    }
    }
    
  },
  
  jianyi(){
    this.setData({
      tishi:false
    });
   this.setData({showjianyi:true});
    
  },
  closejianyi(event){
    this.setData({showjianyi:false});
  },
  xiao(){
      wx.showToast({
        title: '还没写呢',
      })
  },
  showPopup :function (res) {
    let index=res.currentTarget.id;
    let item='show['+index+']';
    this.setData({ [item]:true });
  },
  xiangqing:function(res){
    let index=res.currentTarget.id;
    let item='show['+index+']';
    wx.setStorageSync('taskxq', index);
    wx.navigateTo({
      url: '../xxtask/xxtask',
    })
    this.setData({
      [item]:false
    })
  },
  onClose :function(res) {
    let index=res.currentTarget.id;
    let item='show['+index+']';

    this.setData({ [item]:false });
    
  },
  showPopup1(){
    this.setData({tishi:true});
  },
  onClose1(){
    this.setData({tishi:false})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
     
  },
     
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setStorageSync('shownum', this.data.shownum)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      actions:[{name:'全部任务'},]
    })
    wx.cloud.callFunction({
      name:"openid"
    }).then(res=>{
     
      this.setData({
        userid:res.result.openid
      })
      db.collection("task").where({
        userid:this.data.userid
      }).get().then(res=>{
        this.setData({
          task:res.data
        }
        );
        
        for(var index in this.data.task){
          var st=false
          var month1=this.data.task[index].starttime.getMonth()+1
          var month2=this.data.task[index].endtime.getMonth()+1
          var day1=this.data.task[index].starttime.getDate()
          var day2=this.data.task[index].endtime.getDate()
          this.setData({
            starttime:this.data.starttime.concat(month1+"/"+day1),
            endtime:this.data.endtime.concat(month2+"/"+day2)
          })
          for(var index2 in this.data.task[index].target){
            for(var index1 in this.data.actions){
                if(this.data.actions[index1].name==this.data.task[index].target[index2])
                    st=true;
            }
            if(!st){
              this.setData({
                actions:this.data.actions.concat({"name":this.data.task[index].target[index2]})
              })
            }}
        }
        
        
      })

    })
     wx.hideLoading({
       success: (res) => {},
     })
      
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    for(var index in this.data.show){
      let item='show['+index+']';
      this.setData({[item]:false});
    }
    this.setData({
      tishi:false,
      showxuanxiang:false,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
})